//---------------Sonic CD BrickBlock Script---------------//
//-------Scripted by Twanvanb1--------//

// Aliases
#alias Object.Value0	:	Object.Timer
#alias Object.Value1	:	Object.XVelocity
#alias Object.Value2	:	Object.YVelocity
#alias Object.Value3	:	Object.Stood
#alias Object.Value4	:	Object.FallDelay

#alias Object[24].PropertyValue	:	HUD.CurrentTimePeriod

// Global SFX
#alias 22	:	SFX_G_EXPLOSION
#alias 1	:	GRAVITY_AIR

// Property Values
#alias 0	:	Block
#alias 1	:	VARIANT_1_BIG_LEFT
#alias 2	:	VARIANT_1_BIG_MIDDLE
#alias 3	:	VARIANT_1_BIG_RIGHT

#alias 1 : COL_TOP
// from 8 onwards it will be considered debris


sub ObjectMain
	if Object.PropertyValue > 7
		if Object.FallDelay == 0
			Object.XPos += Object.XVelocity
			Object.YPos += Object.YVelocity
			Object.YVelocity += 0x3800

			if Object.OutOfBounds == true
				Object.Type = TypeName[Blank Object]
			end if
		else
			Object.FallDelay--
		end if
	end if
end sub


sub ObjectPlayerInteraction
	
	switch Object.PropertyValue
	case 0
		PlayerObjectCollision(C_BOX, -16, -16, 16, 16)
		break
	case 1
		if Player.Animation == ANI_JUMPING
			PlayerObjectCollision(C_TOUCH, -16, -16, 16, 16)
			if CheckResult == true
				Player.YVelocity = -0x30000
				Player.Gravity = GRAVITY_AIR
				Object.Type = TypeName[Blank Object]
				PlaySfx(SFX_G_EXPLOSION, false) 
				TempValue0  = Object.PropertyValue
				TempValue0 *= 6
				TempValue0 += 8

				CreateTempObject(TypeName[BrickBlock], TempValue0, Object.XPos, Object.YPos)
				object[TempObjectPos].ixpos -= 8
				object[TempObjectPos].iypos -= 8
				Object[TempObjectPos].YVelocity = -0x20000
				Object[TempObjectPos].XVelocity = -0x20000
				Object[TempObjectPos].DrawOrder =  5

				CreateTempObject(TypeName[BrickBlock], TempValue0, Object.XPos, Object.YPos)
				object[TempObjectPos].ixpos += 8
				object[TempObjectPos].iypos -= 8
				Object[TempObjectPos].YVelocity = -0x20000
				Object[TempObjectPos].XVelocity = 0x20000
				Object[TempObjectPos].DrawOrder =  5
				TempValue0++
				
				CreateTempObject(TypeName[BrickBlock], TempValue0, Object.XPos, Object.YPos)
				object[TempObjectPos].ixpos -= 8
				object[TempObjectPos].iypos += 8
				Object[TempObjectPos].YVelocity = -0x10000
				Object[TempObjectPos].XVelocity = -0x10000
				Object[TempObjectPos].DrawOrder =  5
				TempValue0++

				CreateTempObject(TypeName[BrickBlock], TempValue0, Object.XPos, Object.YPos)
				object[TempObjectPos].ixpos += 8
				object[TempObjectPos].iypos += 8
				Object[TempObjectPos].YVelocity = -0x10000
				Object[TempObjectPos].XVelocity = 0x10000
				Object[TempObjectPos].DrawOrder =  5
				TempValue0++
			end if
		else
			PlayerObjectCollision(C_BOX, -16, -16, 16, 16)
		end if
		break
	case 2
		PlayerObjectCollision(C_BOX, -16, -16, 16, 16)
			if CheckResult == COL_TOP
				Object.Stood = true
			end if
			if Object.Stood == true
				Object.Timer++
				if Object.Timer == 16
					Object.Type = TypeName[Blank Object]
					PlaySfx(SFX_G_EXPLOSION, false) 
					TempValue0  = Object.PropertyValue
					TempValue0 *= 6
					TempValue0 += 8

					CreateTempObject(TypeName[BrickBlock], TempValue0, Object.XPos, Object.YPos)
					object[TempObjectPos].ixpos -= 8
					object[TempObjectPos].iypos -= 8
					Object[TempObjectPos].YVelocity = 0x20000
					Object[TempObjectPos].DrawOrder =  5
					Object[TempObjectPos].FallDelay =  16
					TempValue0++

					CreateTempObject(TypeName[BrickBlock], TempValue0, Object.XPos, Object.YPos)
					object[TempObjectPos].ixpos += 8
					object[TempObjectPos].iypos -= 8
					Object[TempObjectPos].YVelocity = 0x20000
					Object[TempObjectPos].DrawOrder =  5
					Object[TempObjectPos].FallDelay =  12
					TempValue0++

					TempValue3 += 0x100000
					
					CreateTempObject(TypeName[BrickBlock], TempValue0, Object.XPos, Object.YPos)
					object[TempObjectPos].ixpos -= 8
					object[TempObjectPos].iypos += 8
					Object[TempObjectPos].YVelocity = 0x20000
					Object[TempObjectPos].DrawOrder =  5
					Object[TempObjectPos].FallDelay =  8
					TempValue0++

					CreateTempObject(TypeName[BrickBlock], TempValue0, Object.XPos, Object.YPos)
					object[TempObjectPos].ixpos += 8
					object[TempObjectPos].iypos += 8
					Object[TempObjectPos].YVelocity = 0x20000
					Object[TempObjectPos].DrawOrder =  5
					Object[TempObjectPos].FallDelay =  4
					TempValue0++

					TempValue3 += 0x10 0000
				end if
			end if
		break
	end switch
end sub



sub ObjectDraw
	DrawSprite(0)
	if Object.PropertyValue == 0
		DrawSprite(1)
	else
	end if
	if Object.PropertyValue == 1
		DrawSprite(1)
	else
	end if
	if Object.PropertyValue == 2
		DrawSprite(1)
	else
	end if
end sub


sub ObjectStartup
	LoadSpriteSheet("R2/Objects2.gif")

	// Variant 1
	TempValue0 = HUD.CurrentTimePeriod
	TempValue1 = 1
	TempValue0 *= 33
	TempValue1 += TempValue0 
	SpriteFrame(-8, -8, 16, 16, TempValue1, 66)			// #0 - Destoed Block
	SpriteFrame(-16, -16, 32, 32, TempValue1, 66)		// #1 - BrickBlock
	
	// Variant 1 debris chunks

	// Debris chunks BreakWall singular piece
	TempValue0 = 0
	TempValue3 = 1
	while TempValue0 < 3
		TempValue1 = 0
		TempValue2 = 190
		while TempValue1 < 2
			SpriteFrame(-8, -8, 16, 16, TempValue2, TempValue3)
			TempValue1++
			TempValue2 += 16
		loop
		TempValue0++
		TempValue3 += 16
	loop

	// Debris chunks BreakWall left piece
	TempValue0 = 0
	TempValue3 = 1
	while TempValue0 < 3
		TempValue1 = 0
		TempValue2 = 223
		while TempValue1 < 2
			SpriteFrame(-8, -8, 16, 16, TempValue2, TempValue3)
			TempValue1++
			TempValue2 += 16
		loop
		TempValue0++
		TempValue3 += 16
	loop

	// Debris chunks BreakWall middle piece
	TempValue0 = 0
	TempValue3 = 50
	while TempValue0 < 3
		TempValue1 = 0
		TempValue2 = 190
		while TempValue1 < 2
			SpriteFrame(-8, -8, 16, 16, TempValue2, TempValue3)
			TempValue1++
			TempValue2 += 16
		loop
		TempValue0++
		TempValue3 += 16
	loop

	// Debris chunks BreakWall right piece
	TempValue0 = 0
	TempValue3 = 50
	while TempValue0 < 3
		TempValue1 = 0
		TempValue2 = 223
		while TempValue1 < 2
			SpriteFrame(-8, -8, 16, 16, TempValue2, TempValue3)
			TempValue1++
			TempValue2 += 16
		loop
		TempValue0++
		TempValue3 += 16
	loop
	// original code have this below LoadSpriteSheet, moved for consistency with the rest
	ArrayPos0 = 32
	while ArrayPos0 < 1056
		if Object[ArrayPos0].Type == TypeName[BrickBlock]
			Object[ArrayPos0].DrawOrder = 4
		end if
		ArrayPos0++
	loop
end sub

sub RSDKEdit
	if editor.returnVariable == true
		switch editor.variableID
		case 0 // property value
			checkResult = object.propertyValue
			break
		end switch
	else
		switch editor.variableID
		case 0 // property value
			object.propertyValue = editor.variableValue
			break
		end switch
	end if
end sub

sub RSDKDraw
	DrawSprite(0)
	TempValue0 = 16; TempValue1 = 16; TempValue2 = 16; TempValue3 = 16;
	CallFunction(EditorHelpers_DrawHitbox)

	if editor.showGizmos == true
		editor.drawingOverlay = true

		if object.propertyValue == 2
			TempValue0 = object.ypos
			TempValue0 += 0x400000
			DrawArrow(object.xpos, object.ypos, object.xpos, TempValue0, 255, 255, 255)
		end if
		editor.drawingOverlay = false
	end if
end sub


sub RSDKLoad
	LoadSpriteSheet("R2/Objects2.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 66)		// #0 - BrickBlock

	AddEditorVariable("breakType")
	SetActiveVariable("breakType")
	AddEnumVariable("solid", 0)
	AddEnumVariable("break from top", 1)
	AddEnumVariable("falling", 2)
end sub